const cp = require('cp');
require('./page.css');
let moduleExport = {};
function getTime(time, type) {
  // 倒计时60s
  switch (type) {
    case 5:
      if (time === 1) {
        vm.send = true;
        vm.time = '';
        vm.codetxt = '发送验证码';
        time = 120;
      } else {
        time--;
        vm.codetxt = '';
        vm.time = time;
        setTimeout(function() {
          getTime(time, type);
        }, 1000);
        vm.send = false;
      }
      break;
    case 1:
      if (time === 1) {
        vm.sends = true;
        vm.time_two = '';
        vm.codetxt2 = '发送验证码';
        time = 120;
      } else {
        time--;
        vm.codetxt2 = '';
        vm.time_two = time;
        setTimeout(function() {
          getTime(time, type);
        }, 1000);
        vm.sends = false;
      }
      break;
    case 2:
      if (time === 1) {
        vm.sends3 = true;
        vm.time_3 = '';
        vm.codetxt3 = '发送验证码';
        time = 120;
      } else {
        time--;
        vm.codetxt3 = '';
        vm.time_3 = time;
        setTimeout(function() {
          getTime(time, type);
        }, 1000);
        vm.sends3 = false;
      }
      break;
  }
}
let vm = avalon.define({
  $id: 'mc_loginpop',
  module: '',
  checked: true,
  phone_number: '',
  validate_number: '',
  new_password: '',
  mobile: '',
  mobile_forget: '',
  vcode: '',
  username: '',
  password: '',
  login_opt: 1,
  qr_code: 1,
  register: 1,
  send: false,
  sends: false,
  sends3: false,
  time: '',
  time_two: '',
  time_3: '',
  codetxt: '发送验证码',
  codetxt2: '发送验证码',
  codetxt3: '发送验证码',
  getValidate: (res) => {
    if (vm.register === 1) {
      if (!isNaN(res.target.value) && res.target.value.length === 11) {
        vm.send = true;
      } else {
        vm.send = false;
      }
    } else if (vm.register === 3) {
      if (!isNaN(res.target.value) && res.target.value.length === 11) {
        vm.sends3 = true;
      } else {
        vm.sends3 = false;
      }
    } else {
      if (!isNaN(res.target.value) && res.target.value.length === 11) {
        vm.sends = true;
      } else {
        vm.sends = false;
      }
    }
  },
  bind_validate: () => {
    if (vm.send || vm.sends || vm.sends3) {
      let m;
      if (vm.register === 1) {
        vm.module = 5; // 登录
        m = vm.mobile;
      } else if (vm.register === 3) {
        vm.module = 2; // 忘记密码
        m = vm.mobile_forget;
      } else {
        vm.module = 1;
        m = vm.phone_number;
      }
      console.log('222');
      cp.ajaxUrl({
        controller: 'account',
        cmd: 'com.rel.porg.pb.acc_send_verfiy_code_req',
        checkUser: false,
        data: {
          mobile: m,
          module: vm.module,
        },
        success: res => {
          cp.layerMsg('短信验证码发送成功');
          getTime(120, vm.module);
        },
        fail: res => {
          cp.layerMsg(res.desc);
        },
      });
    }
  },
  bindclose: () => {
    vm.phone_number = '';
    vm.validate_number = '';
    vm.new_password = '';
    vm.mobile = '';
    vm.vcode = '';
    vm.username = '';
    vm.password = '';
    vm.send = false;
    vm.sends = false;
    vm.sends3 = false;
    vm.login_opt = 2;
    vm.qr_code = 1;
    vm.register = 1;
    cp.layerCloseAll();
  },
  validate_mobile: {
    onValidateAll: function(reasons) {
      // 获取用户列表
      if (reasons.length) {
        cp.layerMsg({
          msg: reasons[0].getMessage(),
        });
      } else {
        cp.ajaxUrl({
          controller: 'account',
          cmd: 'com.rel.porg.pb.acc_sign_in_req',
          checkUser: false,
          data: {
            account: vm.mobile,
            vcode: vm.vcode,
          },
          success: res => {
            cp.setCookie('token', res.token, 12);
            const libs = require('libs');
            libs.sendParamBetweenPages('common', 'orgid', res.info.orgid);
            libs.sendParamBetweenPages('common', 'username', res.info.name);
            libs.sendParamBetweenPages('common', 'userhead', res.info.head);
            if (res.token) {
              window.location.href = cp.PAGE_ROOT + 'index/index/page.html';
            }
          },
          fail: res => {
            cp.layerMsg(res.desc);
          },
        });
      }
    },
  },
  validate_login: {
    onValidateAll: function(reasons) {
      // 获取用户列表
      if (reasons.length) {
        cp.layerMsg({
          msg: reasons[0].getMessage(),
        });
      } else {
        let md5 = require('js-md5');
        let Base64 = require('js-base64').Base64;
        cp.ajaxUrl({
          controller: 'account',
          checkUser: false,
          cmd: 'com.rel.porg.pb.acc_sign_in_req',
          data: {
            account: vm.username,
            passwd: Base64.encode(md5(vm.password).toUpperCase()),
          },
          success: function(res) {
            cp.setCookie('token', res.token, 12);
            const libs = require('libs');
            libs.sendParamBetweenPages('common', 'orgid', res.info.orgid);
            libs.sendParamBetweenPages('common', 'username', res.info.name);
            libs.sendParamBetweenPages('common', 'userhead', res.info.head);
            if (res.token) {
              window.location.href = cp.PAGE_ROOT + 'index/index/page.html';
            }
          },
          fail: (res) => {
            cp.layerMsg({
              msg: res.desc,
            });
          },
        });
      }
    },
  },
  validate_register: {
    onValidateAll: function(reasons) {
      // 获取用户列表
      if (reasons.length) {
        cp.layerMsg({
          msg: reasons[0].getMessage(),
        });
      } else {
        let md5 = require('js-md5');
        let Base64 = require('js-base64').Base64;
        cp.ajaxUrl({
          controller: 'account',
          checkUser: false,
          cmd: 'com.rel.porg.pb.acc_sign_up_req',
          data: {
            mobile: vm.phone_number,
            vcode: vm.validate_number,
            passwd: Base64.encode(md5(vm.new_password).toUpperCase()),
          },
          success: function(res) {
            cp.setCookie('token', res.token, 12);
            const libs = require('libs');
            libs.sendParamBetweenPages('common', 'orgid', res.info.orgid);
            libs.sendParamBetweenPages('common', 'username', res.info.name);
            libs.sendParamBetweenPages('common', 'userhead', res.info.head);
            if (res.token) {
              window.location.href = cp.PAGE_ROOT + 'index/index/page.html';
            }
          },
          fail: (res) => {
            cp.layerMsg({
              msg: res.desc,
            });
          },
        });
      }
    },
  },
  validate_forgotPw: {
    onValidateAll: function(reasons) {
      // 获取用户列表
      if (reasons.length) {
        cp.layerMsg({
          msg: reasons[0].getMessage(),
        });
      } else {
        let md5 = require('js-md5');
        let Base64 = require('js-base64').Base64;
        cp.ajaxUrl({
          controller: 'account',
          checkUser: false,
          cmd: 'com.rel.porg.pb.acc_mobile_repwd_req',
          data: {
            mobile: vm.mobile_forget,
            vcode: vm.validate_number,
            passwd: Base64.encode(md5(vm.new_password).toUpperCase()),
          },
          success: function(res) {
            cp.setCookie('token', res.token, 12);
            const libs = require('libs');
            libs.sendParamBetweenPages('common', 'orgid', res.info.orgid);
            libs.sendParamBetweenPages('common', 'username', res.info.name);
            libs.sendParamBetweenPages('common', 'userhead', res.info.head);
            if (res.token) {
              window.location.href = cp.PAGE_ROOT + 'index/index/page.html';
            }
          },
          fail: (res) => {
            cp.layerMsg({
              msg: res.desc,
            });
          },
        });
      }
    },
  },
});

moduleExport.content = require('./content.html');
moduleExport.vm = vm;
module.exports = moduleExport;
